package com.fmboy.MXH.nav.controller;

import com.fmboy.MXH.nav.domain.AdminRequired;
import com.fmboy.MXH.nav.domain.ErrorCode;
import com.fmboy.MXH.nav.domain.Result.Result;
import com.fmboy.MXH.nav.domain.dto.UserDTO;
import com.fmboy.MXH.nav.domain.pojo.UserVO;
import com.fmboy.MXH.nav.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@AdminRequired
@RestController
@Tag(name = "管理员用户接口", description = "adminUser API")
@RequestMapping("/mxh-nav/admin/user")
public class adminUserController {

    @Resource
    private UserService userService;

    @Operation(summary = "获取-所有用户", description = "获取-所有用户")
    @GetMapping()
    public Result<List<UserVO>> getAllUser(@RequestParam(required = false) String username,
                             @RequestParam(required = false) String email) {


        try {
            List<UserVO> allUser = userService.getAllUser(username, email);
            return Result.success("获取成功！", allUser);
        } catch (Exception e) {
            return Result.error(ErrorCode.INVALID_REQUEST, "获取失败！");
        }

    }

    @Operation(summary = "获取-用户详情", description = "获取-用户详情")
    @GetMapping("/info")
    public Result<UserVO> getUserInfo(@RequestParam Integer id) {

        try {
            UserVO userInfo = userService.getUserInfo(id);
            return Result.success("获取成功！", userInfo);
        } catch (Exception e) {
            return Result.error(ErrorCode.INVALID_REQUEST, "获取失败！");
        }

    }

    @Operation(summary = "添加-用户", description = "添加-用户")
    @PostMapping()
    public Result<String> addUser(@RequestBody UserDTO userDTO) {

        try {
            userService.addUser(userDTO);
            return Result.success("添加成功！");
        } catch (Exception e) {
            return Result.error(ErrorCode.INVALID_REQUEST, e.getMessage());
        }

    }

    //修改-用户信息
    @Operation(summary = "修改-用户信息", description = "修改-用户信息")
    @PutMapping
    public Result<String> updateUser(@RequestBody UserDTO userDTO) {

        try {
            userService.AdminUpdateUserInfo(userDTO);
            return Result.success("修改成功！");
        } catch (Exception e) {
            return Result.error(ErrorCode.INVALID_REQUEST, e.getMessage());
        }
    }

    //修改-用户密码
    @Operation(summary = "修改-用户密码", description = "修改-用户密码")
    @PutMapping("/password")
    public Result<String> updatePassword(@RequestBody UserDTO userDTO) {

        try {
            userService.AdminUpdateUserPassword(userDTO);
            return Result.success("修改成功！");
        } catch (Exception e) {
            return Result.error(ErrorCode.INVALID_REQUEST, e.getMessage());
        }
    }

    //修改-用户角色
    @Operation(summary = "修改-用户角色", description = "修改-用户角色")
    @PutMapping("/role")
    public Result<String> updateRole(@RequestBody UserDTO userDTO) {

        try {
            userService.AdminUpdateUserRole(userDTO);
            return Result.success("修改成功！");
        } catch (Exception e) {
            return Result.error(ErrorCode.INVALID_REQUEST, e.getMessage());
        }
    }

    @Operation(summary = "删除-用户", description = "删除-用户")
    @DeleteMapping
    public Result<String> deleteUser(@RequestParam Integer id) {

        try {
            userService.AdminUserById(id);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(ErrorCode.INVALID_REQUEST, e.getMessage());
        }
    }
}
